using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using JiravaNet.Web.UI.Fieldset;
using System.Web.Mvc.Html;

namespace JiravaNet.Web.UI.Fieldset {
	/// <summary>
	/// Trida obsahujici builder, ktery spravuje definici polozek
	/// </summary>
	public class FieldsetBuilder: IFieldsetBuilder, IFieldsetControlBuilder, IEnumerable<FieldsetItem> {

		private readonly List<FieldsetItem> _items;
		private FieldsetItem _currentItem;

		/// <summary>
		/// ctor.
		/// </summary>
		public FieldsetBuilder() {
			_items = new List<FieldsetItem>();
		}

		#region Implementation of IFieldsetBuilder

		/// <summary>
		/// Vytvori novou polozku a prevezme jeji definici 
		/// </summary>
		/// <param name="header">Nadpisek polozky</param>
		/// <param name="name">oznaceni formularove polozky</param>
		/// <returns>Fluent interface</returns>
		public IFieldsetControlBuilder For(string header, string name) {
			_currentItem = new FieldsetItem {
			                                	Header = header,
			                                	Name = name
			                                };
			_items.Add(_currentItem);
			return this;
		}

		/// <summary>
		/// Vytvori novou polozku a prevezme jeji definici 
		/// </summary>
		/// <param name="value">Hodnota polozky</param>
		/// <returns>Fluent interface</returns>
		public IFieldsetSimpleItemBuilder For(string value) {
			_currentItem = new FieldsetItem {
			                                	Value = value
			                                };

			_items.Add(_currentItem);
			return this;
		}

		#endregion

		#region Implementation of IFieldsetItemBuilder

		/// <summary>
		/// Urcuje, co se ma s aktualne vytvorenou polozkou udelat
		/// </summary>
		/// <param name="action">provadena akce</param>
		/// <returns>Fluent interface</returns>
		public IFieldsetItemBuilder Do(Func<HtmlHelper, string> action) {
			_currentItem.Actions.Add(action);
			return this;
		}

		public IFieldsetSimpleItemBuilder Validate() {
			string name = _currentItem.Name;
			Func<HtmlHelper, string> action = h => h.ValidationMessage(name);
			_currentItem.Actions.Add(action);
			return this;
		}

		public IFieldsetSimpleItemBuilder Validate(string message) {
			string name = _currentItem.Name;
			Func<HtmlHelper, string> action = h => h.ValidationMessage(name, message);
			_currentItem.Actions.Add(action);
			return this;
		}

		public IFieldsetSimpleItemBuilder Validate(string message, object htmlAttributes) {
			string name = _currentItem.Name;
			Func<HtmlHelper, string> action = h => h.ValidationMessage(name, message, htmlAttributes);
			_currentItem.Actions.Add(action);
			return this;

		}

		#endregion

		#region Implementation of IEnumerable

		/// <summary>
		/// Returns an enumerator that iterates through the collection.
		/// </summary>
		/// <returns>
		/// A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.
		/// </returns>
		/// <filterpriority>1</filterpriority>
		public IEnumerator<FieldsetItem> GetEnumerator() {
			return _items.GetEnumerator();
		}

		/// <summary>
		/// Returns an enumerator that iterates through a collection.
		/// </summary>
		/// <returns>
		/// An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.
		/// </returns>
		/// <filterpriority>2</filterpriority>
		IEnumerator IEnumerable.GetEnumerator() {
			return _items.GetEnumerator();
		}

		#endregion

		#region Implementation of IFieldsetControlBuilder

		public IFieldsetItemBuilder CheckBox() {
			string name = _currentItem.Name;
			Do(h => h.CheckBox(name));
			return this;
		}

		public IFieldsetItemBuilder CheckBox(object htmlAttribues) {
			string name = _currentItem.Name;
			Do(h => h.CheckBox(name, htmlAttribues));
			return this;
		}

		public IFieldsetItemBuilder DropDownList(SelectList source, string optionLabel) {
			string name = _currentItem.Name;
			Do(h => h.DropDownList(optionLabel, name, source));
			return this;
		}

		public IFieldsetItemBuilder DropDownList(SelectList source, string optionLabel, object htmlAttribues) {
			string name = _currentItem.Name;
			Do(h => h.DropDownList(optionLabel, name, source, htmlAttribues));
			return this;
		}

		public IFieldsetItemBuilder Hidden(object value) {
			string name = _currentItem.Name;
			Do(h => h.Hidden(name, value));
			return this;
		}

		public IFieldsetItemBuilder Hidden(object value, object htmlAttribues) {
			string name = _currentItem.Name;
			Do(h => h.Hidden(name, value, htmlAttribues));
			return this;
		}

		public IFieldsetItemBuilder ListBox(MultiSelectList source) {
			string name = _currentItem.Name;
			Do(h => h.ListBox(name, source));
			return this;
		}

		public IFieldsetItemBuilder ListBox(MultiSelectList source, object htmlAttributes) {
			string name = _currentItem.Name;
			Do(h => h.ListBox(name, source, htmlAttributes));
			return this;
		}

		public IFieldsetItemBuilder Password(object value) {
			string name = _currentItem.Name;
			Do(h => h.Password(name, value));
			return this;
		}

		public IFieldsetItemBuilder Password(object value, object htmlAttributes) {
			string name = _currentItem.Name;
			Do(h => h.Password(name, value, htmlAttributes));
			return this;
		}

		public IFieldsetControlBuilder RadioButton(object value, bool isChecked) {
			string name = _currentItem.Name;
			Do(h => h.RadioButton(name, value, isChecked));
			return this;
		}

		public IFieldsetControlBuilder RadioButton(object value, bool isChecked, object htmlAttributes) {
			string name = _currentItem.Name;
			Do(h => h.RadioButton(name, value, isChecked, htmlAttributes));
			return this;
		}

		public IFieldsetItemBuilder TextArea(string value) {
			string name = _currentItem.Name;
			Do(h => h.TextArea(name, value));
			return this;
		}

		public IFieldsetItemBuilder TextArea(string value, object htmlAttributes) {
			string name = _currentItem.Name;
			Do(h => h.TextArea(name, value, htmlAttributes));
			return this;
		}

		public IFieldsetItemBuilder TextArea(string value, int rows, int columns, object htmlAttributes) {
			string name = _currentItem.Name;
			Do(h => h.TextArea(name, value, rows, columns, htmlAttributes));
			return this;
		}

		public IFieldsetItemBuilder TextBox(object value) {
			string name = _currentItem.Name;
			Do(h => h.TextBox(name, value));
			return this;
		}

		public IFieldsetItemBuilder TextBox(object value, object htmlAttributes) {
			string name = _currentItem.Name;
			Do(h => h.TextBox(name, value, htmlAttributes));
			return this;
		}

		public IFieldsetItemBuilder Submit(string text) {
			string name = _currentItem.Name;
			Do(h => h.Submit(name, text));
			return this;
		}

		public IFieldsetItemBuilder Submit(string text, object htmlAttributes) {
			string name = _currentItem.Name;
			Do(h => h.Submit(name, text));
			return this;
		}

		#endregion
	}
}